<?php
/**
 * @param $form
 * @param $form_state
 * @param $slide_show
 * @return mixed
 */
function cao_profile_form($form, &$form_state, $slide_show) {
    global $user;

    // During initial form build, add the entity to the form state for use during
    // form building and processing. During a rebuild, use what is in the form
    // state.
    if (!isset($form_state['slide_show'])) {
        $form_state['slide_show'] = $slide_show;
    }
    else {
        $slide_show = $form_state['slide_show'];
    }

    // @todo Legacy support. Modules are encouraged to access the entity using
    //   $form_state. Remove in Drupal 8.
    $form['#slide_show'] = $slide_show;
//    $form['#user_category'] = $category;

//    if ($category == 'account') {
    cao_account_form($form, $form_state);
        // Attach field widgets.
//        $langcode = entity_language('user', $account);
//        field_attach_form('user', $account, $form, $form_state, $langcode);
//    }

    $form['actions'] = array('#type' => 'actions');
    $form['actions']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Save'),
    );
//    if ($category == 'account') {
        $form['actions']['cancel'] = array(
            '#type' => 'submit',
            '#value' => t('Cancel account'),
            '#submit' => array('slide_show_edit_cancel_submit'),
//            '#access' => $account->uid > 1 && (($account->uid == $user->uid && user_access('cancel account')) || user_access('administer users')),
        );
//    }

//    $form['#validate'][] = 'user_profile_form_validate';
    // Add the final user profile form submit handler.
    $form['#submit'][] = 'user_profile_form_submit';

    return $form;
}

/**
 * Submit function for the user account and profile editing form.
 */
function slide_show_edit_cancel_submit($form, &$form_state) {
    $slide_show = $form_state['slide_show'];
//    $category = $form['#slide_show'];
    // Remove unneeded values.
    form_state_values_clean($form_state);

    // Before updating the account entity, keep an unchanged copy for use with
    // user_save() later. This is necessary for modules implementing the user
    // hooks to be able to react on changes by comparing the values of $account
    // and $edit.
    $slide_show_unchanged = clone $slide_show;

    entity_form_submit_build_entity('cao_slideshow', $slide_show, $form, $form_state);

    // Populate $edit with the properties of $account, which have been edited on
    // this form by taking over all values, which appear in the form values too.
    $edit = array_intersect_key((array) $slide_show, $form_state['values']);

    slide_show_save($slide_show_unchanged);
    $form_state['values']['id'] = $slide_show->id;

//    if (!empty($edit['pass'])) {
//        // Remove the password reset tag since a new password was saved.
//        unset($_SESSION['pass_reset_'. $account->uid]);
//    }
    // Clear the page cache because pages can contain usernames and/or profile information:
    cache_clear_all();

    drupal_set_message(t('The changes have been saved.'));
}